import * as constants from './constants';
import { fromJS } from 'immutable';
import { getWebApiConfigsAxios,webApiTestConAxios,webApiSaveConAxios,deleteWebConAxios,webApiGetConAxios,editWebConAxios } from '../../../../api/dataSource/apiManage'

import { message } from 'antd';

const getConfig = (webApi) => ({
  type: constants.GET_API_CONFIG,
  webApi: fromJS(webApi),
});

export const getAllConfigOperation = (userName) => {
  return(dispatch) => {
    getWebApiConfigsAxios(userName).then((res) => {
      if(res){
        if(res.data.ok){
          const webApi = res.data.data;
          console.log(webApi)
          dispatch(getConfig(webApi));
        }
      }
    });
  }
};

export const searchConfigOperation = (userName,searchValue) => {
  return(dispatch) => {
    getWebApiConfigsAxios(userName).then((res) => {
      if(res){
        if(res.data.ok){
          const config = res.data.data;
          let filterData = config.filter(item => JSON.parse(item.userDatabaseConfig).configName.includes(searchValue));
          dispatch(getConfig(filterData));
        }
      }
    });
  }
};

const getPreview = (preview) => ({
  type: constants.GET_PREVIEW,
  preview: fromJS(preview)
});

export const testConOperation = (data) => {
  return(dispatch) => {
    webApiTestConAxios(data).then((res) => {
      if(res){
        if(res.data.ok){
          const preview = res.data.data.entity;
          console.log(preview)
          dispatch(getPreview(preview));
        }
      }
    });
  }
}

export const saveConOperation = (data,searchValue) => {
  return(dispatch) => {
    webApiSaveConAxios(data).then((res) => {
      if(res){
        if(res.data.ok){
          message.success("添加成功！");
          if(searchValue===""){
            dispatch(getAllConfigOperation(data.username));
          }
          else{
            dispatch(searchConfigOperation(data.username,searchValue));
          }
        }
        else{
          message.error("添加失败！",res.data.msg)
        }
      }
    });
  }
}

export const editConOperation = (params,searchValue) => {
  return(dispatch)=>{
    editWebConAxios(params).then((res)=>{
      if(res){
        if(res.data.ok){
          message.success("编辑成功！");
          if(searchValue===""){
            dispatch(getAllConfigOperation(params.username));
          }
          else{
            dispatch(searchConfigOperation(params.username,searchValue));
          }
        }
        else{
          message.error("编辑失败！",res.data.msg);
        }
      }
    });
  }
}

export const clearPreviewOperation = () => {
  return(dispatch) => {
    dispatch(getPreview({}));
  }
}

export const getCon = (config) => ({
  type: constants.GET_CON,
  config: fromJS(config)
})

export const getConOperation = (id) => {
  return(dispatch)=>{
    webApiGetConAxios(id).then((res)=>{
      if(res){
        if(res.data.ok){
          const config = res.data.data;
          console.log(config)
          dispatch(getCon(config));
        }
      }
    });
  }
}

export const clearConfigOperation = () => {
  return(dispatch)=>{
    dispatch(getCon({}));
  }
}

export const deleteConOperation = (params) => {
  return(dispatch)=>{
    deleteWebConAxios(params).then((res)=>{
      if(res){
        if(res.data.ok){
          message.success("删除成功！")
          dispatch(getAllConfigOperation());
        }
        else{
          message.error("删除失败！",res.data.msg)
        }
      }
    });
  }
}